package com.nullpointer.xchat.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import de.ankri.views.Switch;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.utils.DataManager;

public class SettingDialogFragment extends DialogFragment {
	private Switch swNotification;
	private Switch swSound;
	private Switch swVibrate;
	private Button btAction;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_setting, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		swNotification.setChecked(DataManager.getInstance(getActivity())
				.getNotificationSetting());
		swSound.setChecked(DataManager.getInstance(getActivity())
				.getSoundSetting());
		swVibrate.setChecked(DataManager.getInstance(getActivity())
				.getVibrationSetting());
	}

	private void setEvent() {
		btAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		swNotification
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						DataManager.getInstance(getActivity())
								.setNotificationSetting(isChecked);
					}
				});

		swSound.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				DataManager.getInstance(getActivity()).setSoundSetting(
						isChecked);
			}
		});

		swVibrate.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				DataManager.getInstance(getActivity()).setVibrationSetting(
						isChecked);
			}
		});
	}

	private void findViewIds(View view) {
		swNotification = (Switch) view.findViewById(R.id.sw_notification);
		swSound = (Switch) view.findViewById(R.id.sw_sound);
		swVibrate = (Switch) view.findViewById(R.id.sw_vibrate);
		btAction = (Button) view.findViewById(R.id.bt_action);
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
	}

}
